package sam.learn.framework.web;

import java.io.Serializable;

public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 6909071286269686685L;

    public static final int SUCCESS = 0;

    public static final int FAIL = -1;

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(int code, String message, T data) {
        this();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功
     *
     * @param message 消息内容
     * @param data    消息体
     * @return 响应
     */
    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<T>(SUCCESS, message, data);
    }

    /**
     * 成功
     *
     * @param message 消息内容
     * @return 响应
     */
    public static <T> ResponseResult<T> success(String message) {
        return new ResponseResult<T>(SUCCESS, message, null);
    }

    /**
     * 失败
     *
     * @param message 消息内容
     * @param data    消息体
     * @return 响应
     */
    public static <T> ResponseResult<T> fail(String message, T data) {
        return new ResponseResult<T>(FAIL, message, data);
    }

    /**
     * 失败
     *
     * @param message 消息内容
     * @return 响应
     */
    public static <T> ResponseResult<T> fail(String message) {
        return new ResponseResult<T>(FAIL, message, null);
    }

    /**
     * 获取 响应码
     *
     * @return code 响应码
     */
    public int getCode() {
        return this.code;
    }

    /**
     * 设置 响应码
     *
     * @param code 响应码
     */
    public void setCode(int code) {
        this.code = code;
    }

    /**
     * 获取 响应消息
     *
     * @return message 响应消息
     */
    public String getMessage() {
        return this.message;
    }

    /**
     * 设置 响应消息
     *
     * @param message 响应消息
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 获取 响应数据
     *
     * @return data 响应数据
     */
    public T getData() {
        return this.data;
    }

    /**
     * 设置 响应数据
     *
     * @param data 响应数据
     */
    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseResult{" + "code=" + code + ", message='" + message + '\'' + ", data=" + data + '}';
    }
}
